import os
from google.appengine.ext.webapp import template  
  
import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Information(db.Model):
  job = db.StringProperty(multiline=True)
  school = db.StringProperty(multiline=True)
  gpa = db.StringProperty(multiline=True)
  region = db.StringProperty(multiline=True)
  

class MainPage(webapp.RequestHandler):
  def get(self):
      categories = { 'jobs': [ "Choose a Field", "Accounting", "Art, Entertainment, and Gaming", "Construction", "Education", "Engineering", "Lawyer", "Management", "Manufacturing", "Marketing", "Media and Broadcasting", "Military", "Pharmacy", "Physician", "Real Estate", "Science and Biotech"],
              'schools' : ["Choose Your School", "Brown University", "California Institute of Technology", "Carnegie Mellon University", "Columbia University", "Cornell University", "Dartmouth College", "Duke University", "Georgetown University", "Georgia Institute of Technology", "Harvard University", "Johns Hopkins University", "Massachusetts Institute of Technology", "Michigan State University", "New York University", "Northwestern University", "Princeton University", "Purdue University", "Rice University", "Stanford University", "University of Arizona", "University of California, Berkeley", "University of Chicago", "University of Pennsylvania", "University of Southern California", "Vanderbilt University", "Yale University"],
              'GPAs' : ["Choose Your GPA", "2.0", "2.5", "3.0", "3.5", "4.0"],
              'regions' : ["Choose Your Region", "Midwest", "Northeast", "Southeast", "Southwest", "West Coast"]}
      path = os.path.join(os.path.dirname(__file__), 'index.html')
      self.response.out.write(template.render(path, categories))
      
class InfoSubmit(webapp.RequestHandler):
    def get(self):
        info = Information()
    
        info.job = self.request.get('cat_job')
        info.school = self.request.get('cat_school')
        info.gpa = self.request.get('cat_gpa')
        info.region = self.request.get('cat_region')
    
        info.put()
        self.redirect('/result?usrfield=%s&usrschool=%s&usrgpa=%s&usrregion=%s' % (info.job, info.school, info.gpa, info.region))


class Result(webapp.RequestHandler):
  def get(self):
        information = Information()
       
        Rjob = self.request.get('usrfield')
        Rschool = self.request.get('usrschool')
        Rgpa = self.request.get('usrgpa')
        Rregion = self.request.get('usrregion')
        
        salary_score = 0
                
        if Rjob == "Military" or Rjob == "Media and Broadcasting":
            salary_score = 30000  
        elif Rjob == "Accounting" or Rjob == "Manufacturing":
            salary_score = 43000
        elif Rjob == "Art, Entertainment, and Gaming" or Rjob == "Education" or Rjob == "Construction":
            salary_score = 52000   
        elif Rjob == "Science and Biotech" or Rjob == "Marketing" or Rjob == "Engineering":
            salary_score = 65000
        elif Rjob == "Lawyer":
            salary_score = 90000
        elif Rjob == "Pharmacy" or Rjob == "Real Estate":
            salary_score = 110000
        elif Rjob == "Physician" or Rjob == "Management":
            salary_score = 140000
           
        if Rschool == "Michigan State University" or Rschool == "University of Arizona":
            salary_score = salary_score*1.01
        elif Rschool == "Northwestern University" or Rschool == "Purdue University" or Rschool == "Vanderbilt University" or Rschool == "Yale University":
            salary_score = salary_score*1.02
        elif Rschool == "Dartmouth College" or Rschool == "Duke University" or Rschool == "Georgetown University" or Rschool == "Johns Hopkins University" or Rschool == "Columbia University" or Rschool == "New York University" or Rschool == "Rice University" or Rschool == "University of California, Berkeley":
            salary_score = salary_score*1.03
        elif Rschool == "Brown University" or Rschool == "Georgia Institute of Technology" or Rschool == "Harvard University" or Rschool == "University of Pennsylvania" or Rschool == "University of Southern California":
            salary_score = salary_score*1.04
        elif Rschool == "Carnegie Mellon University" or Rschool == "Stanford University" or Rschool == "Princeton University" or Rschool == "Cornell University":
            salary_score = salary_score*1.05
        elif Rschool == "California Institute of Technology" or Rschool == "Massachusetts Institute of Technology":
            salary_score = salary_score*1.06
        
        if Rregion == "West Coast":
            salary_score = salary_score*1.2
        if Rregion == "Midwest":
            salary_score = salary_score*.85
        if Rregion == "Southwest":
            salary_score = salary_score*.85     
            
        if Rgpa == "2.0":
            salary_score = salary_score - 6000
        elif Rgpa == "2.5":
            salary_score = salary_score - 3000
        elif Rgpa == "3.5":
            salary_score = salary_score + 3000
        elif Rgpa == "4.0":
            salary_score = salary_score + 6000
        
        result_variables = { 'salary' : ["Your salary will be ", int(salary_score) ," per year"],
                   'Gjob' : Rjob, 'Gschool' : Rschool, 'Ggpa' : Rgpa, 'Gregion' : Rregion }
           
        path = os.path.join(os.path.dirname(__file__), 'result.html')
        self.response.out.write(template.render(path, result_variables))
        
       
class Stats(webapp.RequestHandler, db.Model):
    def get(self):
        Hjob = self.request.get('Pjob')
        Hschool = self.request.get('Pschool')
        Hgpa = self.request.get('Pgpa')
        Hregion = self.request.get('Pregion')
        
        query1 = Information.gql("WHERE job='%s'" % Hjob)
        JC = query1.count()
        
        query2 = Information.gql("WHERE school='%s'" % Hschool)
        SC = query2.count()
        
        query3 = Information.gql("WHERE gpa='%s'" % Hgpa)        
        GC = query3.count()
        
        query4 = Information.gql("WHERE region='%s'" % Hregion)        
        RC = query4.count()
        
        stats_variables = {'jobCount' : JC, 'schoolCount' : SC, 'gpaCount' : GC, 'regionCount' : RC}
        
        path = os.path.join(os.path.dirname(__file__), 'stats.html')
        self.response.out.write(template.render(path, stats_variables))



application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/submit', InfoSubmit),
                                      ('/result', Result),
                                      ('/stats', Stats)],
                                     debug=True)


def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()